require(["config"],function(config){
	require(["jquery","cookie","pagination"],function(jq,cookie,pagination){
	
		$(function(){					
			$(".nav_menu a").mouseenter(function(){			
				var index = $(this).index();
				$(".nav_menu .headlist").eq(index).css("display","block" 	)				             
			})				
			$(".nav_menu").mouseleave(function(){
				$(".nav_menu .headlist").css("display","none");
				
			})	

			$.ajax({
				type:"get",
				url:"js/list.json",
				async:true,
				success:function(data){
				
					$.each(data["list"], function(i,item) {
						$("<li class='list-item'><a href='details.html?id='" + i + 
						" class='list1' href='' target='_blank'><img width='200' height='200' src='" + item["src"] + "'/></a><a class='list2' href='' style='display: block; width: 198px;' target='_blank'>"
				+ item["describe"] + "</a><p class='list3'><span class='jg'><em class='dol'>￥</em>" + item["price"] +"</span><span class='list_scj'>￥" + item["prc"] + 
				"</span><em class='cx-tags'>赠</em><em class='cx-tags'>加</em></p><p class='list4'><a class='pl_num'target='_blank' href=''><em></em>"
				+ item["people"] + "篇评价</a></p><p class='list5'><a class='shop' id='" + i + 
				"'>加入购物车</a><a class='save' href=''>关注</a></p><div class='prompt'><div class='tankuang clear'><b></b><div><h2>商品已成功添加到购物车！</h2><p><a class='jisuan' href=''>去购物车结算</a><a class='jixu' style='cursor:pointer'>继续购物</a></p></div><a style='cursor:pointer' class='close'></a></div></div></li>")
					.appendTo($(".rg_sl_list_ul")); 
					});
				}
		
		})
			new Choice();
			new Trick();	
		})
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		function shop(){
		this.sc_car();
		var _this = this;
		$('.rg_sl_list_ul').on('click','.shop',function(){
			//console.log(this)
			_this.click($(this));	
		})
	}
	shop.prototype.click = function(ele){
			$('.prompt').css('display','none');
			ele.parent().next().css('display','block');
			//console.log($(this).parent().next())
			$('.jisuan').on('click',function(){
				ele.parent().parent().find('.prompt').css('display','none');
			})
			$('.jixu').on('click',function(){
				//console.log(ele.parent().parent().find('.prompt'))
				ele.parent().parent().find('.prompt').css('display','none');
			})
			//console.log(ele.id)//正确获取id
			var id = ele[0].id
			//console.log(id)
			var first = $.cookie('goods')==null?true:false;//判断是否有cookie进行添加
			var same = false;//判断是否已经追加
			//是否是第一次添加
			if(first){
				//第一次添加,建立json结构。
				$.cookie('goods','[{id:'+id+',num:1}]',{path:'/'});	
			}else{
				var str = $.cookie('goods');
				var arr = eval(str);
				//遍历所有对象。如果id相同，让该商品数量递增 ;
				for(var attr in arr){
					if(arr[attr].id == id){		
						arr[attr].num = arr[attr].num + 1;  //让json结构中num自增。
						var cookieStr = JSON.stringify(arr);//将json对象转换成字符串.
						$.cookie('goods',cookieStr,{path:'/'});
						same = true;
					}
				}
				//如果id不同，重新建立商品对象;			
				if(!same){
					var obj  = {id:id,num:1};
					arr.push(obj);
					var cookieStr = JSON.stringify(arr);
					$.cookie('goods',cookieStr,{path:'/'});
				}
			}
		this.sc_car();
	}
	shop.prototype.sc_car = function(){
		var sc_str = $.cookie('goods');
		if(sc_str){//如果购物车cookie不为空。
			var sc_obj = eval(sc_str);
			var sc_num = 0 ; 
			for(var i in sc_obj){
				sc_num += Number(sc_obj[i].num);
			}
			//console.log(sc_num)
			$('.sc_num').html(sc_num);
		}
		this.sc_msg();
	}
	shop.prototype.sc_msg = function(){
		var _this = this;
		$.ajax({
			url:'js/list.json',
			type:'GET',
			success:function(res){
				_this.success(res);	
			}
		})
	}
	shop.prototype.success = function(res){
		var a = [];
		for(var j = 0 ; j < 15 ; j++){
			for(var k = 0 ; k < res.length ; k++){
				if(k < res.length){
					a.push(res[k]);
				}else{
					k = 0;
				}
			}	
		}
		//console.log(a)
		var sc_str = $.cookie('goods');
		if(sc_str){
			var sc_obj = eval(sc_str);
			//console.log(sc_obj)
			var sc_num = 0 ;
			var pay = 0 ;
			var html = '';
			//console.log(sc_obj)
			for(var i in sc_obj){	
				//console.log(i)				
				html += '<li class="car_li"><a class="car_l1" target="_blank" href=""><img src="'+ a[sc_obj[i].id].src +'"></a><span class="car_pro_detail"><a class="car_l2" target="_blank" href="">'
				+ a[sc_obj[i].id].describe +'</a><span class="car_l3"><i class="car_l3_1">￥'+ a[sc_obj[i].id].price +'</i>x<em>'+sc_obj[i].num+'</em></span></span><span class="car_l3_2"><a>删除</a></span></li>';
				pay += a[sc_obj[i].id].price * sc_obj[i].num;
			}
			$('#shopping_car_ul').html(html);
			$('.all_price').html(pay);
		}
		//console.log($('.car_l3_2'))
		this.clickl();
	}
	shop.prototype.clickl = function(){
		var _this = this;
		$('.car_l3_2>a').on('click',function(event){
			//console.log($(this))
			_this.clicka(event,$(this));			
		})
	}
	shop.prototype.clicka = function(event,ele){
		//console.log(event.target)
		if(event.target == ele[0]){
			var str = $.cookie('goods');
			var arr = eval(str);
			arr.splice(ele.parent().parent().index(),1);
			var cookieStr = JSON.stringify(arr);
			$.cookie('goods',cookieStr,{path:'/'});
			ele.parent().parent() .remove();
			if($.cookie('goods')){
				this.sc_car();
			}
		}
	}


	function Trick(){
		//console.log($('.trick'))
		var _this = this;
		$('.trick').on('click',function(){
			_this.change($(this));
			//console.log($(this))
		})
	}
	Trick.prototype.change = function(who){
		if(who.parent().next().height()!=0){
			//console.log(1)
			who.parent().next().css({
				'display':'none',
				height:0
			})
			who.css('background-position','-49px -31px')
		}else{
			who.parent().next().css({
				'display':'block',
				height:'auto'
			})
			who.css('background-position','-67px -31px')
		}
	}

	function Choice(){
		//console.log($('.choice'));
		$('.choice').on('click',function(){
			//console.log(event.target)
			//console.log($(this).parent().next())
			$(this).parent().nextAll().css('display','block');
		})
		//console.log($('.mul_btn2'))
		$('.mul_btn2').on('click',function(){
			$(this).parent().parent().css('display','none');
		})
	}

		
		
	});
});



	


